<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title></title>
    {include file='header'}
</head>

<body>
    <div class="x-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label label-required">标题</label>
                <div class="layui-input-block">
                    <input type="text" id="title" name="title" value="{if !empty($data)}{$data.title}{/if}"
                        lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类</label>
                <div class="layui-input-block">
                    <div class="flex">
                    <select name="category_id" id="category_id" style="flex:1" lay-filter="categoryFilter">
                        {foreach $category as $v}
                        <option value="{$v.id}"{if !empty($data)}{if $data.category_id==$v.id} selected{/if}{/if}>{$v.spl}{$v.name}</option>
                            
                        {/foreach}
                    </select>
                    <button type="button" id="addBtn"style="padding:3px 8px;background-color:#eee;line-height:30px;border:1px solid #eee">+</button>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-label label-required">图片</label>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="images">
                            <i class="layui-icon">&#xe67c;</i>上传图片
                        </button>
                        <div class="demo-images">{if !empty($data.images)}
                            {foreach $data.images as $v}
                            <div class="upload-list"><img data-url="{$v}" src="{$v}" class="upload-img"
                                    ondblclick="removeimg(this)" /><span class="upload-remove"
                                    onclick="remove_img(this)">✖</span></div>
                            {/foreach}
                            {/if}
                        </div>
                        <input name="images" type="hidden" value="{$data.images_str|default=''}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>内容</label>
                <div class="layui-input-block">
                    <textarea name="contents" id="contents"
                        data-url="{:Url('wadmin/ueditor/index', ['path_type'=>'article'])}">{if !empty($data)}{$data.contents}{/if}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" value="1" lay-text="开启|停用" {if empty($data)} checked=""
                        {else}{if $data.status==1} checked="" {/if}{/if} lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                <input type="hidden" id="id" name="id" value="{$data.id|default=''}" />
                <input type="hidden" id="parent_id" name="parent_category_id" value="{$parent_id}" />
                <button type="button" class="layui-btn btn-fix" lay-filter="save" lay-submit="">保存</button>
                </div>
            </div>
        </form>
    </div>
    <!-- ueditor 编辑器 -->
    <script type='text/javascript' src="/lib/ueditor/ueditor.config.js"></script>
    <script type='text/javascript' src="/lib/ueditor/ueditor.all.js"></script>
    <script type='text/javascript' src="/lib/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript">

        var upload_editor = UE.getEditor("contents", {
            zIndex: 0,
            serverUrl: "{:url('wadmin/Ueditor/index', ['path_type'=>'article/'])}",
            focus: false,
            enableAutoSave: false,
            autoSyncData: false,
            autoFloatEnabled: false,
            wordCount: false,
            sourceEditor: null,
            scaleEnabled: true,
            toolbars: [[
                'fullscreen', 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'simpleupload', 'insertimage', 'fontborder', 'subscript', 'removeformat', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
                'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
                'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
                'link', 'unlink', 'horizontal', 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'preview', 'help'
            ]]
        });
        layui.use(['form','upload'], function () {
            var form = layui.form;
            // 点击按钮添加选项
          $('#addBtn').click(function(){
            layer.prompt({
                formType: 0,
                title: '请输入分类名称',
                value: '', // 默认显示值
                maxlength: 50, 
                anim: 5
            }, function(value, index, elem){
                $.post('../category/save',{name:value,parent_id:{$parent_id}},function(res){
                    if(res.code==1)
                    {
                        layer.msg('设置成功',{time:500},function(){
                            $("#category_id").append('<option value="'+res.data.id+'">'+value+'</option>')
                            $("#category_id").val(res.data.id);
                            form.render('select');//,'categoryFilter');
                            layer.close(index);
                        });
                       
                        return
                    }
                    layer.msg(res.msg);
                })
                
            });
            // 必须重新渲染
            form.render('select');
          });
            var upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#images'
                , url: "{:url('wadmin/upload/index')}"
                , data: { type: "article" }//额外传输的参数
                // ,exts: 'jpg|png|gif|' //可传输文件的后缀
                // ,size: 100 //传输大小100k
                // ,headers:{token:'XXXXXXXX'} //额外添加的请求头
                // ,multiple: false //多文件上传
                , done: function (res) {
                    console.log(res)
                    if (res.code == 1) {
                        var imgname = $(".demo-images").html();
                        var filepath = res.data.url;
                        var imgo = '<div class="upload-list"><img data-url="' + filepath + '" src="' + filepath + '" class="upload-img" ondblclick="removeimg(this)"/><span class="upload-remove" onclick="remove_img(this)">✖</span></div>'
                        imgname = imgname + imgo;
                        $(".demo-images").html(imgname);
                        regetimg();
                    }
                    else {
                        layer.msg(res.msg)
                    }
                }
                , error: function () {
                    layer.msg('上传失败');
                }
            });
        });
        UE.getEditor('contents').addListener('blur', function (editor) { UE.getEditor("contents").sync(); });
        layui.use('form', function () {
            var form = layui.form;
            form.render()
            //监听提交
            form.on('submit(save)', function (data) {
                UE.getEditor("contents").sync();
                console.log(data.field)
                $.ajax({
                    type: "POST",
                    url: "{:url('save')}",
                    data: data.field,
                    datatype: "json",
                    success: function (res, textStatus) {
                        console.log(res)
                        if (res.code == 1) {
                            layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                window.parent.location.reload();
                                parent.layer.close(index);
                            });
                        }
                        else {
                            layer.msg(res.msg, { icon: 5 });//0感叹号  1打勾  2打叉  3问号  4锁头  5难过  6笑脸
                            return false;
                        }
                    },
                    error: function (e) {
                        layer.alert('系统出现错误,请检查系统！', { icon: 2 });
                        return false;
                    }
                });
                return false;
            });
        });
    </script>
{include file="footer"}